/**
 * 
 */
package com.nari.baseinfo.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.nari.baseinfo.model.ICity;
import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class CityDaoImpl extends GenericDaoImpl<ICity, String> implements ICityDao {

	/**
	 * 查询所有一级行政机构
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-2
	 */
	public List<ICity> queryFirstLevelCity() throws Exception {
		String hsql = "from ICity c " +
				"where c.cityLevel=1 " +
				"order by c.orderID,c.cityNamePinyin";
		return this.runQueryHSql(hsql);
	}
	
	/**
	 * 根据父机构查询所有子机构列表
	 * @param parentID 父机构(可选)，若不存在，则查询一级行政机构
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-2
	 */
	public List<ICity> queryCityByParentID(String parentID) throws Exception {
		StringBuffer hsql = new StringBuffer("from ICity c where ");
		if(parentID!=null && !"".equals(parentID.trim())) {
			hsql.append("c.parentID='");
			hsql.append(parentID);
			hsql.append("' ");
		} else {
			hsql.append("c.cityLevel=1 ");
		}
		hsql.append("order by c.orderID,c.cityNamePinyin");
		return this.runQueryHSql(hsql.toString());
	}
	
	/**
	 * 分页查询行政机构列表
	 * @param parentID 父机构(可选)，若不存在，则查询一级行政机构
	 * @param cityName 行政机构名称(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-2
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<ICity> queryCityPageByParentID(String parentID, String cityName, 
			PageCond pageCond) throws Exception {
		StringBuffer cityQueryHql = new StringBuffer("from ICity c where ");
		//父机构ID
		if(parentID!=null && !"".equals(parentID.trim())) {
			cityQueryHql.append("c.parentID='");
			cityQueryHql.append(parentID);
			cityQueryHql.append("'");
		} else {
			cityQueryHql.append("c.cityLevel=1");
		}
		//行政区划名称
		if(cityName!=null && !"".equals(cityName.trim())) {
			cityQueryHql.append(" and lower(c.cityName) like '%");
			cityQueryHql.append(cityName.toLowerCase());
			cityQueryHql.append("%'");
		}
		//结果集个数查询
		String cityCountHql = "select count(*) " + cityQueryHql.toString();
		int total = ((Long)this.getCurrentSession().createQuery(cityCountHql).uniqueResult()).intValue();
		//结果集排序
		cityQueryHql.append(" order by c.orderID,c.cityNamePinyin");
		//机构列表分页查询
		Query query = this.getCurrentSession().createQuery(cityQueryHql.toString());
		query.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
		List<ICity> cityList = query.list();
		//构造返回结果
		pageCond.setTotal(total);
		ResultWithPageCond<ICity> cityPage = new ResultWithPageCond<ICity>();
		cityPage.setPageCond(pageCond);
		cityPage.setList(cityList);
		return cityPage;
	}
	
	/**
	 * 根据父机构唯一标识等信息统计机构个数
	 * @param parentID 父机构唯一标识(可选)，若不存，则统计所有行政机构的个数
	 * @param cityName 机构名称(可选)
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-5
	 */
	public int countCityByParent(String parentID, String cityName) throws Exception {
		Criteria criteria = this.getCurrentSession().createCriteria(ICity.class);
		if(parentID!=null && !"".equals(parentID.trim())) {
			criteria.add(Restrictions.eq("parentID", parentID.trim()));
		} 
		if(cityName!=null && !"".equals(cityName.trim())) {
			criteria.add(Restrictions.ilike("cityName", cityName.trim().toLowerCase(), MatchMode.ANYWHERE));
		}
		return (Integer) criteria.setProjection(Projections.rowCount()).list().get(0);
	}
	
	/**
	 * 通过机构ID列表查询机构信息
	 * @param idList 机构ID列表，若列表为空，则返回空列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-6
	 */
	public List<ICity> queryCityByIDList(List<String> idList) throws Exception {
		if(idList!=null && idList.size()>0) {
			StringBuffer hsql = new StringBuffer("select distinct c from ICity c ");
			hsql.append("where c.id in(");
			for(int index=0; index<idList.size(); index++) {
				hsql.append("'");
				hsql.append(idList.get(index));
				hsql.append("'");
				if(index < idList.size()-1) {
					hsql.append(",");
				}
			}
			hsql.append(") ");
			hsql.append("order by c.orderID,c.cityNamePinyin");
			return this.runQueryHSql(hsql.toString());
		}
		return null;
	}
	
	/**
	 * 通过机构ID列表查询机构ID-机构名称映射表
	 * @param idList 机构ID列表，若列表为空，则返回一个空映射表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-6
	 */
	public Map<String, String> queryIdNameMapByIDList(List<String> idList) throws Exception {
		Map<String, String> idNameMap = new HashMap<String, String>();
		if(idList!=null && idList.size()>0) {
			StringBuffer hsql = new StringBuffer("select distinct c.id,c.cityName from ICity c ");
			hsql.append("where c.id in(");
			for(int index=0; index<idList.size(); index++) {
				hsql.append("'");
				hsql.append(idList.get(index));
				hsql.append("'");
				if(index < idList.size()-1) {
					hsql.append(",");
				}
			}
			hsql.append(")");
			List<Object> resultsList = this.runQueryHSqlObject(hsql.toString());
			//格式化查询结果集
			if(resultsList!=null && resultsList.size()>0) {
				for(Object result: resultsList) {
					Object[] arrayMap = (Object[]) result;
					idNameMap.put(arrayMap[0].toString(), arrayMap[1].toString());
				}
			}
		}
		return idNameMap;
	}
	
}
